﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BusStopsManager.aspx.cs" Inherits="GPSTrackingWebsiteManager.Pages.BusStopsManager" %>

<!DOCTYPE html>
<link href="../Css/GPSTrackingWebsite.css" rel="stylesheet" />
<script src="../Javascript/WindowMessageManager.js"></script>
<script src="../Javascript/ObjectsV2.js"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Administrador de Paradas de Buses</title>
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
        <script type="text/javascript">

            var opener = GetRadWindow();
            var BusStopsManager = new BusStopsManager();
            var Companies = new Companies();

            function GetRadWindow() {
                var oWindow = null;
                if (window.radWindow)
                    oWindow = window.radWindow;
                else if (window.frameElement.radWindow)
                    oWindow = window.frameElement.radWindow;
                return oWindow.BrowserWindow;
            }

            function getCompaniesByAdminUsername() {
                PageMethods.wsGetCompaniesByAdminUsername(getCompaniesByAdminUsernameOk, getCompaniesByAdminUsernameError);
            }

            function getCompaniesByAdminUsernameOk(object) {
                Companies.openXMLCompaniesByAdminUsername(object, opener.globalBusStopsManager.companiesList);
                Companies.loadCompaniesDataSource(getCompaniesGrid(), opener.globalBusStopsManager.companiesList, opener.globalBusStopsManager.companiesDataSource);
                if (opener.globalBusStopsManager.companiesList.length == 1) {
                    getCompaniesGrid().selectItem(0);
                    if (BusStopsManager.isValidToGetBusRoutes(opener, opener.globalBusStopsManager.companiesList[0].companyID, getBusRoutesGrid())) {
                        getBusRoutesByCompanyID();
                    }
                }
            }

            function getCompaniesByAdminUsernameError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                msgError(error.get_message(), titles.busStopsTitle);
            }

            function CompaniesGrid_OnRowClick(sender, args) {
                if (BusStopsManager.isValidToGetBusRoutes(opener, args.getDataKeyValue("CompanyID"), getBusRoutesGrid())) {
                    requestStart(getrpvBusStopsManagerUpdateControl());
                    BusStopsManager.clearBusRoutes(opener);
                    BusStopsManager.clearSelection(getBusStopsGrid(), opener.globalBusStopsManager.busStopsList);
                    clearList(opener.globalBusStopsManager.busStopsList);
                    clearDataSource(opener.globalBusStopsManager.busStopsDataSource, getBusStopsGrid(), true);
                    getBusRoutesByCompanyID();
                }
            }

            function getBusRoutesByCompanyID() {
                PageMethods.wsGetBusRoutesByCompanyID(opener.globalBusStopsManager.companyID, getBusRoutesByCompanyIDOk, getBusRoutesByCompanyIDError);
            }

            function getBusRoutesByCompanyIDOk(object) {
                BusStopsManager.openXMLBusRoutesByCompanyID(object, opener.globalBusStopsManager.busRoutesList);
                loadBusRoutesDataSourceWait();
                responseEnd(getrpvBusStopsManagerUpdateControl());
            }

            function getBusRoutesByCompanyIDError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                msgError(error.get_message(), titles.busStopsTitle);
            }

            function loadBusRoutesDataSourceWait() {
                BusStopsManager.loadBusRoutesDataSource(getBusRoutesGrid(), opener.globalBusStopsManager.busRoutesList, opener.globalBusStopsManager.busRoutesDataSource);
                responseEnd(getrpvBusStopsManagerUpdateControl());
            }

            function BusRoutesGrid_OnRowClick(sender, args) {
                if (BusStopsManager.isValidToGetBusStop(opener, args.getDataKeyValue("BusRouteID"), getBusStopsGrid())) {
                    requestStart(getrpvBusStopsManagerUpdateControl());
                    getBusStopsByCompanyIDAndBusRouteID();
                }
            }
            
            function getBusStopsByCompanyIDAndBusRouteID() {
                PageMethods.wsGetBusStopsByCompanyIDAndBusRouteID(opener.globalBusStopsManager.companyID, opener.globalBusStopsManager.busRouteID, getBusStopsByCompanyIDAndBusRouteIDOk, getBusStopsByCompanyIDAndBusRouteIDError);
            }

            function getBusStopsByCompanyIDAndBusRouteIDOk(object) {
                BusStopsManager.openXMLBusStopsByCompanyAndBusRouteID(opener.objMapControl, object, opener.globalBusStopsManager.busStopsList);
                BusStopsManager.loadBusStopDataSource(getBusStopsGrid(), opener.globalBusStopsManager.busStopsList, opener.globalBusStopsManager.busStopsDataSource);
                GetAllBusRouteDetailsByBusRouteID();
                for (i = 0; i < opener.globalBusStopsManager.busStopsList.length; i++) {
                    getBusStopsGrid().selectItem(i);
                    
                    opener.globalBusStopsManager.busStopsList[i].checked = true;
                    if (getCkbCenterBusStops().checked) {
                        opener.objMapControl.centerMap(opener.objMapControl.getPositionMarker(opener.globalBusStopsManager.busStopsList[i].marker));
                    }

                    if (getCkbZoomBusStops().checked) {
                        opener.objMapControl.zoomMap(16);
                    }
                    BusStopsManager.showAllBusStopsChecked(opener.globalBusStopsManager.busStopsList);
                }
            }

            function getBusStopsByCompanyIDAndBusRouteIDError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                msgError(error.get_message(), titles.busStopsTitle);
            }

            function GetAllBusRouteDetailsByBusRouteID() {
                PageMethods.wsGetAllBusRouteDetailsByBusRouteID(opener.globalBusStopsManager.busRouteID, GetAllBusRouteDetailsByBusRouteIDOk, GetAllBusRouteDetailsByBusRouteIDError);
            }

            function GetAllBusRouteDetailsByBusRouteIDOk(object) {
                BusStopsManager.clearBusRoutes(opener);
                BusStopsManager.openXMLBusRouteDetailsByBusRouteID(object, opener.globalBusStopsManager);

                opener.globalBusStopsManager.temporalyIndex = 0;
                opener.globalBusStopsManager.temporalyInterval = setInterval(function () { loadBusRouteDisplay(opener); }, opener.globalBusStopsManager.timeToCalculateRoute);
            }

            function loadBusRouteDisplay(opener) {
                if (opener.globalBusStopsManager.temporalyIndex < opener.globalBusStopsManager.temporalyList.length) {
                    opener.objMapControl.addBusRouteClickDB(opener.globalBusStopsManager.temporalyList[opener.globalBusStopsManager.temporalyIndex++]);
                }
                else {
                    clearInterval(opener.globalBusStopsManager.temporalyInterval);
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                }
            }

            function GetAllBusRouteDetailsByBusRouteIDError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                msgError(error.get_message(), titles.busStopsTitle);
            }

            function BusStopsGrid_RowSelecting(sender, args) {
                BusStopsManager.busStopGridRowSelecting(opener, args, getCkbZoomBusStops().checked, getCkbCenterBusStops().checked);
            }

            function BusStopsGrid_OnRowClick(sender, args) {
                if (BusStopsManager.busStopGridOnRowClick(opener, args)) {
                    BusStopDeleteCompletely();
                }
            }

            function BusStopDeleteCompletely() {
                requestStart(getrpvBusStopsManagerUpdateControl());
                getBusStopsGrid().clearSelectedItems();

                if (opener.globalBusStopsManager.busStopID != -1) {
                    PageMethods.wsBusStopDelete(opener.globalBusStopsManager.busRouteID, opener.globalBusStopsManager.busStopID, BusStopDeleteCompletelyOk, BusStopDeleteCompletelyError);
                }
                else {
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    msgValidation("Debe seleccionar una parada de bus para eliminarlo.", titles.busStopsTitle);
                }
            }

            function BusStopDeleteCompletelyOk(object) {
                if (object = "true") {
                    BusStopsManager.refreshAfterDeleteBusStop(opener, getBusStopsGrid());
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    msgInformation("Se eliminó correctamente la parada de bus.", titles.busStopsTitle);
                }
                else {
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    msgError("No se eliminó correctamente la parada de bus.", titles.busStopsTitle);
                }
            }

            function BusStopDeleteCompletelyError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                msgError(error.get_message(), titles.busStopsTitle);
            }

            function BusStopsGrid_RowDeselecting(sender, args) {
                var index = BusStopsManager.getIndexBusStopID(opener.globalBusStopsManager.busStopsList, args.getDataKeyValue("BusStopID"));
                if (index != -1) {
                    opener.globalBusStopsManager.busStopsList[index].checked = false;
                    BusStopsManager.showAllBusStopsChecked(opener.globalBusStopsManager.busStopsList);
                }
            }

            function getBusRoutesGrid() {
                try {
                    return $find("<%= rgBusRoutesGrid.ClientID %>").get_masterTableView();
                }
                catch (ex) {
                    setTimeout(getBusRoutesGrid, 100);
                }
            }

            function getCompaniesGrid() {
                try {
                    return $find("<%= rgCompaniesGrid.ClientID %>").get_masterTableView();
                }
                catch (ex) {
                    setTimeout(getCompaniesGrid, 100);
                }
            }

            function getBusStopsGrid() {
                try {
                    return $find("<%= rgBusStopsGrid.ClientID %>").get_masterTableView();
                }
                catch (ex) {
                    setTimeout(getBusStopsGrid, 100);
                }
            }

            function getrpvBusStopsManagerUpdateControl() {
                return "<%=  rpvBusStopsManager.ClientID %>";
            }

            function enableDisableBusStopHalfTerminalEventClickMap() {
               
                //if (getCkbHalfTerminalStops().checked) {
                    getCkbTerminalStops().checked = false;
                    getCkbBusStops().checked = false;
                //}

                BusStopsManager.enableDisableBusStopEventClickMap(opener, getCkbHalfTerminalStops().checked);
            }

            function enableDisableBusStopTerminalEventClickMap() {
                
                if (getCkbTerminalStops().checked) {
                    getCkbBusStops().checked = false;
                    getCkbHalfTerminalStops().checked = false;
                }

                BusStopsManager.enableDisableBusStopEventClickMap(opener, getCkbTerminalStops().checked);
            }

            function enableDisableBusStopEventClickMap() {

                if (getCkbBusStops().checked)
                {
                    getCkbTerminalStops().checked = false;
                    getCkbHalfTerminalStops().checked = false;
                }

                BusStopsManager.enableDisableBusStopEventClickMap(opener, getCkbBusStops().checked);
            }

            function getRtbBusStopsName() {
                return $get('<%= rtbBusStopsName.ClientID %>');
            }

            function getCkbBusStops() {
                return $get('<%= ckbBusStops.ClientID %>');
            }

            function getCkbTerminalStops() {
                return $get('<%= ckbTerminalStops.ClientID %>');
            }

            function getCkbHalfTerminalStops() {
                return $get('<%= ckbHalfTerminalStops.ClientID %>');
            }

            function getCkbZoomBusStops() {
                return $get('<%= ckbZoomBusStop.ClientID %>');
            }

            function getCkbCenterBusStops() {
                return $get('<%= ckbCenterBusStop.ClientID %>');
            }

            function requestStart(object) {
                try {
                    $find("<%= RadAjaxLoadingPanel1.ClientID%>").show(object);
                }
                catch (e) {
                    null;
                }
            }

            function responseEnd(object) {
                try {
                    $find("<%= RadAjaxLoadingPanel1.ClientID%>").hide(object);
                }
                catch (e) {
                    null;
                }
            }

            function isValidToInsertBusStop() {
                requestStart(getrpvBusStopsManagerUpdateControl());
                var objects = { busStopName: getRtbBusStopsName().value, lat: null, lng: null }
                if (BusStopsManager.isValidToInsertBusStop(opener, objects)) {
                    BusStopInsert(objects);
                }
                else {
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                }
            }

            function BusStopInsert(objects) {
                PageMethods.wsBusStopInsert(opener.globalBusStopsManager.companyID, opener.globalBusStopsManager.busRouteID, objects.busStopName, objects.lat, objects.lng, getCkbTerminalStops().checked, getCkbHalfTerminalStops().checked, BusStopInsertOk, BusStopInsertError);
            }

            function BusStopInsertOk(id) {
                if (id != -1) {
                    BusStopsManager.busStopInsertSuccessful(opener, id, getBusStopsGrid(), getRtbBusStopsName().value);
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    getRtbBusStopsName().value = "";
                    msgInformation("Se registró correctamente la parada de Bus.", titles.busStopsTitle);
                }
                else {
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    msgValidation("El nombre de la parada de Bus ya está registrado para esta compañía, cambiar el nombre de la parada de Bus.", titles.busStopsTitle);
                }
            }

            function BusStopInsertError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                msgError(error.get_message(), titles.busStopsTitle);
            }

        </script>
    </telerik:RadScriptBlock>
    <style type="text/css">
        .auto-style1
        {
            width: 153px;
        }
    </style>
</head>
<body onload="getCompaniesByAdminUsername()">

    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true"></telerik:RadScriptManager>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
					<telerik:AjaxSetting AjaxControlID="AjaxControlID1">
						<UpdatedControls>
							<telerik:AjaxUpdatedControl ControlID="Loading" LoadingPanelID="RadAjaxLoadingPanel1" />
						</UpdatedControls>
					</telerik:AjaxSetting>
				</AjaxSettings>
        </telerik:RadAjaxManager>

        <telerik:RadTabStrip ID="rtsBusStopsManager" runat="server" SelectedIndex="0" MultiPageID="rmpBusStopsManager" Skin="Metro">
        </telerik:RadTabStrip>
        <telerik:RadMultiPage ID="rmpBusStopsManager" runat="server"  SelectedIndex="0">
            <telerik:RadPageView ID="rpvBusStopsManager" runat="server">

                <table style="width: 100%; text-align: left">
		             <tr>
                        <td colspan="3">
                            <fieldset>
								<legend>Nueva Parada de Bus</legend>
								<table class="auto-style2">
									<tr>
										<td class="auto-style1">
											<asp:CheckBox ID="ckbBusStops" runat="server" onclick="javascript:enableDisableBusStopEventClickMap()" Text="Habilitar Parada" Width="170px"/>
                                            <asp:CheckBox ID="ckbTerminalStops" runat="server" onclick="javascript:enableDisableBusStopTerminalEventClickMap()" Text="Habilitar Terminal" Width="170px"/>
                                            <asp:CheckBox ID="ckbHalfTerminalStops" runat="server" onclick="javascript:enableDisableBusStopHalfTerminalEventClickMap()" Text="Habilitar 1/2 Terminal" Width="170px"/>
										</td>
									<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
										<td>Nombre de la Parada:</td>
										<td>
											<telerik:RadTextBox ID="rtbBusStopsName" runat="server" AutoPostBack="false">
											</telerik:RadTextBox>
										</td>
										<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
										<td>
											<input id="btnBusStop" type="button" value="Guardar Parada" onclick="isValidToInsertBusStop();" />
										</td>
                                        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
										<td class="auto-style1">
											<asp:CheckBox ID="ckbZoomBusStop" runat="server" Text="Acercar Parada" Width="150px"/>
                                            <asp:CheckBox ID="ckbCenterBusStop" runat="server" Text="Centrar Parada" Width="150px"/>
										</td>
									</tr>
								</table>
                            </fieldset>
                        </td>
                    </tr>
                    <tr>
                        <td>
                  
				            <fieldset>
					            <legend>Compañias Registradas</legend>
					            <telerik:RadGrid ID="rgCompaniesGrid" runat="server" AllowMultiRowSelection="False" AutoGenerateColumns="False" CellSpacing="0" GridLines="None" Height="100px" OnNeedDataSource="rgCompaniesGrid_NeedDataSource" Skin="Metro" Width="100%">
						            <ClientSettings>
							            <Selecting AllowRowSelect="True" />
							            <ClientEvents OnRowClick="CompaniesGrid_OnRowClick" />
							            <Scrolling AllowScroll="True" UseStaticHeaders="False" />
						            </ClientSettings>
						            <MasterTableView ClientDataKeyNames="CompanyID" NoMasterRecordsText="No hay compañias registradas">
							            <CommandItemSettings ExportToPdfText="Export to PDF" />
							            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
							            </RowIndicatorColumn>
							            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
							            </ExpandCollapseColumn>
							            <Columns>
								            <telerik:GridBoundColumn DataField="CompanyID" FilterControlAltText="Filter Description column" HeaderText="ID" UniqueName="CompanyID" Visible="false">
								            </telerik:GridBoundColumn>
								            <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter Description column" HeaderText="Nombre" Visible="true">
								            </telerik:GridBoundColumn>
							            </Columns>
							            <EditFormSettings>
								            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
								            </EditColumn>
							            </EditFormSettings>
						            </MasterTableView>
						            <FilterMenu EnableImageSprites="true">
						            </FilterMenu>
					            </telerik:RadGrid>
				            </fieldset>
			            </td>
			            <td>
                  
				            <fieldset>
					            <legend>Grupos de Rutas Registrados</legend>
					            <telerik:RadGrid ID="rgBusRoutesGrid" runat="server" AllowMultiRowSelection="False" AutoGenerateColumns="False" CellSpacing="0" GridLines="None" Height="100px" OnNeedDataSource="rgBusRoutesGrid_NeedDataSource" Skin="Metro" Width="100%">
						            <ClientSettings>
							            <Selecting AllowRowSelect="True" />
							            <ClientEvents OnRowClick="BusRoutesGrid_OnRowClick" />
							            <Scrolling AllowScroll="True" UseStaticHeaders="False" />
						            </ClientSettings>
						            <MasterTableView ClientDataKeyNames="BusRouteID" NoMasterRecordsText="No hay Grupos de Rutas registrados">
							            <CommandItemSettings ExportToPdfText="Export to PDF" />
							            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
							            </RowIndicatorColumn>
							            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
							            </ExpandCollapseColumn>
							            <Columns>
								            <telerik:GridBoundColumn DataField="BusRouteID" FilterControlAltText="Filter Description column" HeaderText="ID" UniqueName="BusRouteID" Visible="false">
								            </telerik:GridBoundColumn>
								            <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter Description column" HeaderText="Nombre" Visible="true">
								            </telerik:GridBoundColumn>
							            </Columns>
							            <EditFormSettings>
								            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
								            </EditColumn>
							            </EditFormSettings>
						            </MasterTableView>
						            <FilterMenu EnableImageSprites="true">
						            </FilterMenu>
					            </telerik:RadGrid>
				            </fieldset>
			            </td>
			           <td>
                           <fieldset>
					            <legend>Paradas Registradas</legend>
                           <telerik:RadGrid ID="rgBusStopsGrid" runat="server" AllowMultiRowSelection="True" AutoGenerateColumns="False" CellSpacing="0" GridLines="None" Height="100px" OnNeedDataSource="rgBusStopsGrid_NeedDataSource" Skin="Metro" Width="100%">
                                <ClientSettings>
							            <Selecting AllowRowSelect="True" />
							            <ClientEvents OnRowClick="BusStopsGrid_OnRowClick" OnRowDeselecting="BusStopsGrid_RowDeselecting" OnRowSelecting="BusStopsGrid_RowSelecting" />
							            <Scrolling AllowScroll="True" UseStaticHeaders="False" />
						            </ClientSettings>
						            <MasterTableView ClientDataKeyNames="BusStopID" NoMasterRecordsText="No hay paradas registradas">
							            <CommandItemSettings ExportToPdfText="Export to PDF" />
							            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
							            </RowIndicatorColumn>
							            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
							            </ExpandCollapseColumn>
							            <Columns>
								            <telerik:GridImageColumn ImageUrl="..\Template\Icons\16px\Delete.gif" UniqueName="DeleteColumn">
								            </telerik:GridImageColumn>
								            <telerik:GridBoundColumn DataField="BusStopID" FilterControlAltText="Filter Description column" HeaderText="ID" UniqueName="BusStopID" Visible="false">
								            </telerik:GridBoundColumn>
								            <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter Description column" HeaderText="Nombre" Visible="true">
								            </telerik:GridBoundColumn>
								            <telerik:GridClientSelectColumn HeaderText="Mostrar" UniqueName="chkShowBusStop">
								            </telerik:GridClientSelectColumn>
							            </Columns>
							            <EditFormSettings>
								            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
								            </EditColumn>
							            </EditFormSettings>
						            </MasterTableView>
						            <FilterMenu EnableImageSprites="true">
						            </FilterMenu>

                           </telerik:RadGrid>
                           </fieldset>
                       </td>
		            </tr>
	            </table>
            </telerik:RadPageView>
        </telerik:RadMultiPage>
          
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default"></telerik:RadAjaxLoadingPanel>
    </form>
</body>
</html>
